<?php

if (!defined('IN_DISCUZ')) {
    exit('Access Denied');
}
global $_G;
include DISCUZ_ROOT . './source/plugin/qu_app/ainuo.php';
if ($_GET['image_file'] && $_GET['m'] == 'avatar') {
    $imgdata = explode(',', $_GET['image_file']);
    preg_match("/^(data:\\s*image\\/(\\w+);base64,)/", $_GET['image_file'], $result);
    $sExt = '.' . str_replace('jpeg', 'jpg', $result[2]);
    $imgdata = base64_decode($imgdata[1]);
    $basedir = $_G['setting']['attachurl'];
    $sTempFileName = $basedir . 'qu_app/' . $_G['uid'];
    dmkdir($basedir . 'qu_app');
    error_reporting(0);
    file_put_contents($sTempFileName, $imgdata);
    $dirs = sprintf('%09d', $_G['uid']);
    substr($dirs, 0, 3);
    substr($dirs, 3, 2);
    substr($dirs, 5, 2);
    substr($dirs, 7, 2);
    $dir = array(0 => substr($dirs, 0, 3), 1 => substr($dirs, 3, 2), 2 => substr($dirs, 5, 2), 3 => substr($dirs, 7, 2));
    $avatar_dir = DISCUZ_ROOT . 'uc_server/data/avatar/' . $dir[0] . '/' . $dir[1] . '/' . $dir[2] . '/';
    dmkdir($avatar_dir);
    error_reporting(0);
    clipimga($sTempFileName, $avatar_dir . $dir[3] . '_avatar_big' . $sExt, 200, 200);
    clipimga($sTempFileName, $avatar_dir . $dir[3] . '_avatar_middle' . $sExt, 120, 120);
    clipimga($sTempFileName, $avatar_dir . $dir[3] . '_avatar_small' . $sExt, 48, 48);
    unlink($sTempFileName);
    error_reporting(0);
    echo json_encode(array('status' => 1));
}
$auser['avatar'] = avatar($_G['uid'], 'middle', true);
$auser['avatar_big'] = avatar($_G['uid'], 'big', true);
function clipimga($apath, $target_path = '', $awidth = 200, $aheight = 200, $fixed_orig = '')
{
    $source_info = getimagesize($apath);
    $source_width = $source_info[0];
    $source_height = $source_info[1];
    $source_mime = $source_info['mime'];
    $ratio_orig = $source_width / $source_height;
    if ($fixed_orig == 'width') {
        $aheight = $awidth / $ratio_orig;
    } else {
        if ($fixed_orig == 'height') {
            $awidth = $aheight * $ratio_orig;
        } else {
            if ($awidth / $aheight > $ratio_orig) {
                $awidth = $aheight * $ratio_orig;
            } else {
                $aheight = $awidth / $ratio_orig;
            }
        }
    }
    switch ($source_mime) {
        case 'image/gif':
            $source_image = imagecreatefromgif($apath);
            $target_image = imagecreatetruecolor($awidth, $aheight);
            imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $awidth, $aheight, $source_width, $source_height);
            $imgArr = explode('.', $apath);
            imagejpeg($target_image, $target_path . $imgArr[1], 100);
            break;
        case 'image/jpeg':
            $source_image = imagecreatefromjpeg($apath);
            break;
        case 'image/png':
            $source_image = imagecreatefrompng($apath);
            break;
    }
    return false;
}